﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SACM.Common.Util
{
    public static class Convert
    {
        public static int ToInt(object value)
        {
            if (value == null || value == DBNull.Value || value == "")
            {
                return 0;
            }
            else
            {
                return System.Convert.ToInt32(value);
            }
        }

        public static int? ToNInt(object value)
        {
            int num;
            if (value != null)
            {
                if (int.TryParse(value.ToString(), out num))
                    return System.Convert.ToInt32(value);
            }

            return null;
        }

        public static decimal ToDecimal(object value)
        {
            if (value == null || value == DBNull.Value || value == "")
            {
                return 0;
            }
            else
            {
                return System.Convert.ToDecimal(value.ToString().Replace("$", ""));
            }
        }

        public static decimal? ToNDecimal(object value)
        {
            int num;
            if (value != null)
            {
                if (int.TryParse(value.ToString(), out num))
                    return System.Convert.ToDecimal(value);
            }

            return null;
        }

        public static DateTime ToDateTime(object value)
        {
            if (string.IsNullOrEmpty(value.ToString()) || value == DBNull.Value)
            {
                return new DateTime();
            }

            return System.Convert.ToDateTime(value);
        }

        public static DateTime? ToNDateTime(object value)
        {
            if (string.IsNullOrEmpty(value.ToString()) || value == DBNull.Value)
            {
                return null;
            }

            return System.Convert.ToDateTime(value);
        }
    }
}
